/**
 ******************************************************************************
 * @file    set_wlan.c
 * @author  百问科技
 * @version V1.0
 * @date    2021-10-29
 * @brief	IMX6ULL屏幕背光设置
 ******************************************************************************
 * Change Logs:
 * Date           Author          Notes
 * 2021-10-29     zhouyuebiao     First version
 ******************************************************************************
 * @attention
 *
 * Copyright (C) 2008-2021 深圳百问网科技有限公司<https://www.100ask.net/>
 * All rights reserved
 *
 ******************************************************************************
 */


/*********************
 *      INCLUDES
 *********************/
//#include "../../lv_100ask_demo.h"

#if 1 //LV_USE_100ASK_DEMO_GAME_2048

#include <stdio.h>
#include <string.h>
#include "set_wlan.h"

#include "lvgl/lvgl.h"




static lv_obj_t * g_wifi_conn_info_panel;
static lv_obj_t * g_wifi_conn_list; // 已连接的WiFi列表
static lv_obj_t * g_wifi_scan_list; // 扫描到的wifi列表
static lv_obj_t * g_pwd_ta; // 密码框
lv_obj_t * mbox1;
lv_obj_t * screenWLAN;
static lv_obj_t * screenMain;

char wifiName[32] = {0};//等待连接wifi名
// WLAN信息结构体
static struct WLAN_info {
    char device[8];         // 网卡设备
	int cable;		    // 连接状态
	char dhcp[8];           // DHCPC模式
    char mac[24];           // MAC地址
    char ipaddr[16];        // IP地址
    char gateway[16];       // 网关
    char netmask[16];       // 子网掩码
} WLAN_info = {"0", "0", "0", "0", "0", "0"};


static void wifi_list_event_handler(lv_event_t * e);
bool get_wlan0_info(void);

// 1 : 匹配到字符串  
// 0 ：匹配不到字符串
static bool shell_exec_handle(const char * cmd, const char * result)
{
    FILE * fp;
    char buffer[512] = {0};
    char *str_ret;
    bool ret = 0;

    fp = popen(cmd, "r");
    while (fgets(buffer, 512, fp) != NULL)
    {
        LV_LOG_USER("%s", buffer); 
        if (result != NULL)
        {
            str_ret = strstr(buffer, result);
            if (str_ret != NULL)
            {
                LV_LOG_USER("str_ret != NULL: %s\n", str_ret);
                ret = 1;
            }
        }
    }

    if (pclose(fp) == -1)
    {
        LV_LOG_USER("close failed!");
        //exit(1); // return -1;
        ret = 0;
    }
    return ret;
}


static void shell_opt_handle(const char * cmd, char * result, int readLen)
{
    FILE * fp;
    char buffer[512] = {0};
    int count = 0;
    LV_LOG_USER("cmd=%s, readLen=%d", cmd, readLen);
    fp = popen(cmd, "r");

    
    /*
    fgets（buf，sizeof（buf），resource）
    fgets函数从文件中读取字符，直到遇到换行符（‘\n’）或文件结束标记为止
    sizeof（buf）参数指定fgets将从文件中读取并存储在buf中的最大字符数，以防止缓冲区溢出。
    */
    while (fgets(buffer, sizeof(buffer), fp) != NULL)
    {
        //循环读，在不超过buffer大小的情况下一次读一行文本，直到 fp 的结束位置
        buffer[512] = 0;
        count += strlen(buffer);
        if(count < readLen){
            strcat(result, buffer);
        }else if(count == readLen){
            strcat(result, buffer);
            break;
        }else {
            buffer[strlen(buffer) - (count -readLen)] = 0;
            strcat(result, buffer);
            break;
        }
        memset(buffer, 0, sizeof(buffer));
    }

    if (pclose(fp) == -1)
    {
        LV_LOG_USER("close failed!, cmd=%s", cmd);

    }

}



// 扫描热点
static void wlan_scan()
{

    lv_obj_t * btn;
    char res_buffer[8 * 1024] = {0};
    char *token;

    LV_LOG_USER("\n\n------------------start wlan scan---------------------\n\n");

    shell_opt_handle("/config/wifi/iwlist wlan0 scan", res_buffer, sizeof(res_buffer));
    if (strlen(res_buffer) <= 1)
    {
        LV_LOG_USER("[ERROR]Buffer NULL!");
    }
    LV_LOG_USER("res_buffer=\n%s", res_buffer);
    lv_obj_clean(g_wifi_scan_list); // 清空列表，删除对象的所有子项（但不是对象本身）

    token = strtok(res_buffer, "\n");
    do{
        //LV_LOG_USER("token=%s", token);
        char* result = strstr(token, "ESSID:\"");
        if(result == NULL){
            continue;
        }
        char wifiName[32] = {0};
        char *p = result + strlen("ESSID:\"");
        int i = 0;
        while (*p != '\"'){
            //LV_LOG_USER("*p=%c", *p);
            wifiName[i] = *p;
            i++;
            p++;
        }
        LV_LOG_USER("wifiName=%s", wifiName);//
        if(strlen(wifiName) > 0){
            btn = lv_list_add_btn(g_wifi_scan_list, LV_SYMBOL_RIGHT, wifiName);
            lv_obj_add_event_cb(btn, wifi_list_event_handler, LV_EVENT_CLICKED, NULL);
        }

    }while((token = strtok(NULL, "\n")));

    memset(res_buffer, 0, sizeof(res_buffer));   //清空字符串数组 

}




// 刷新wifi列表
static void refresh_wifi_list_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    //lv_obj_t * obj = lv_event_get_target(e);
    LV_LOG_USER("refresh wifi list");
    if(code == LV_EVENT_CLICKED) {
        wlan_scan();
    }
}


static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);    // 获取事件代码
    lv_obj_t * ta = lv_event_get_target(e);         // 获取事件最初指向的对象
    lv_obj_t * kb = lv_event_get_user_data(e);      // 获取在对象上注册事件时传递的 user_data

    // 判断事件类型(当焦点在键盘上时)
    if(code == LV_EVENT_FOCUSED) {
        //获取输入设备的类型
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);                                   // 为键盘分配一个文本区域
            lv_obj_set_style_max_height(kb, LV_HOR_RES * 2 / 3, 0);             // 设置键盘的最高宽度为屏幕高度的一半
            lv_obj_align_to(g_wifi_conn_info_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);   // 设置对象的高度，当唤出键盘时应该偏移背景
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);                          // 清除标志
            lv_obj_scroll_to_view_recursive(ta, LV_ANIM_OFF);                   // 滚动到一个对象，直到它在其父对象上可见。对父母的父母做同样的事情，依此类推。即使对象具有嵌套的可滚​​动父对象，它也会滚动到视图中
        }
    }
    // 判断事件类型(当焦点不在键盘上时)
    else if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_center(g_wifi_conn_info_panel);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
    // 判断事件类型(当键盘点击确定或取消键)
    else if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
        lv_obj_center(g_wifi_conn_info_panel);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);            // 标志为隐藏对象(使对象隐藏，就像它根本不存在一样)
        lv_obj_clear_state(e->target, LV_STATE_FOCUSED);    // 删除对象的一种或多种状态。其他状态位将保持不变。
        lv_indev_reset(NULL, e->target);                    // 忘记最后一次点击的对象，使其再次成为可关注的对象
    }
}


// 连接wifi
static void conn_wifi(const char * name, char * passwd)
{
    char cmd_buff[256];
    char res_buffer[512] = {0};
    char originalName[32] = {0};
    char originalPassword[32] = {0};
    int count = 0, overrun = 3;

    LV_LOG_USER("wifi name=%s, passwd=%s", name, passwd);
    
    getSaveWifiName(originalName);//得到原wifi名
    getSaveWifiPassword(originalPassword);//得到原密码
    LV_LOG_USER("originalName=%s, originalPassword=", originalName, originalPassword);

    //关wifi
    shell_opt_handle("ifconfig wlan0 down", res_buffer, sizeof(res_buffer));
    //替换wifi名
    memset(cmd_buff, 0, sizeof(cmd_buff));
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "sed -i 's/%s/%s/' /appconfigs/wpa_supplicant.conf", originalName, name);
    shell_opt_handle(cmd_buff, res_buffer, sizeof(res_buffer));
    //替换wifi密码
    memset(cmd_buff, 0, sizeof(cmd_buff));
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "sed -i 's/%s/%s/' /appconfigs/wpa_supplicant.conf", originalPassword, passwd);
    shell_opt_handle(cmd_buff, res_buffer, sizeof(res_buffer));

    //重启wifi
    shell_opt_handle("ifconfig wlan0 up", res_buffer, sizeof(res_buffer));
    shell_opt_handle("/config/wifi/wpa_supplicant -D nl80211 -i wlan0 -c /appconfigs/wpa_supplicant.conf -B &", res_buffer, sizeof(res_buffer));
}



static void conn_btn_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);    // 获取事件代码类型
    lv_obj_t * obj = lv_event_get_target(e);        // 获取触发的对象
    const char * user_data = lv_event_get_user_data(e);   // 获取在对象上注册事件时传递的 user_data

    LV_LOG_USER("user_data=%s\n", user_data);
    if(code == LV_EVENT_CLICKED) {
        if(0 == strcmp(user_data, "back"))
        {
            LV_LOG_USER("wifi name back");
            lv_obj_del(g_wifi_conn_info_panel);                         // 删除对象及其所有子对象
            lv_obj_clear_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);   // 清除标志
            lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_TRANSP, 0);  // 设置透明度
            wlan_scan();
            return;
        }
        
        conn_wifi(user_data, lv_textarea_get_text(g_pwd_ta));

        lv_obj_del(g_wifi_conn_info_panel);                         // 删除对象及其所有子对象
        lv_obj_clear_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);   // 清除标志
        lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_TRANSP, 0);  // 设置透明度

        
    }
}


static void wifi_conn_dialog()
{
    lv_obj_add_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_50, 0);

    LV_LOG_USER("enter");
    //  创建键盘
    lv_obj_t * kb = lv_keyboard_create(lv_layer_top());
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);    // 标志为隐藏对象(使对象隐藏，就像它根本不存在一样)

    // 主区域布局容器
    g_wifi_conn_info_panel = lv_obj_create(lv_layer_top());
    lv_obj_set_size(g_wifi_conn_info_panel, 480, 250);
    lv_obj_set_layout(g_wifi_conn_info_panel, LV_LAYOUT_FLEX);
    lv_obj_set_flex_align(g_wifi_conn_info_panel, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_flex_flow(g_wifi_conn_info_panel, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(g_wifi_conn_info_panel, 0, 0);
    lv_obj_center(g_wifi_conn_info_panel);

    // 输入区域布局容器
    lv_obj_t * input_panel = lv_obj_create(g_wifi_conn_info_panel);
    lv_obj_set_size(input_panel, 380, 150);
    lv_obj_set_style_bg_opa(input_panel, 0, 0);
    lv_obj_set_style_border_opa(input_panel, 0, 0);
    lv_obj_set_layout(input_panel, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(input_panel, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_style_pad_all(input_panel, 0, 0);

    lv_obj_t * pwd_label = lv_label_create(input_panel);
    lv_obj_set_style_text_font(pwd_label, &lv_font_siyuanheiti_normal_20, 0);
    lv_label_set_text(pwd_label, "密码: ");                  // 为标签设置新文本

    g_pwd_ta = lv_textarea_create(input_panel);
    lv_textarea_set_one_line(g_pwd_ta, true);                       // 单行模式
    lv_textarea_set_password_mode(g_pwd_ta, true);                  // 密码模式
    //lv_textarea_set_placeholder_text(g_pwd_ta, "Please input wifi password...");    // 设置文本区域的占位符文本(输入框提示文本内容)
    lv_obj_align(g_pwd_ta, LV_ALIGN_CENTER, 0, 0);                // 更改对象的对齐方式并设置新坐标
    lv_obj_add_event_cb(g_pwd_ta, ta_event_cb, LV_EVENT_ALL, kb);   // 为对象分配事件及其处理函数

    pwd_label = lv_label_create(input_panel);
    lv_label_set_long_mode(pwd_label, LV_LABEL_LONG_SCROLL_CIRCULAR);     /*Circular scroll*/
    lv_obj_set_style_text_font(pwd_label, &lv_font_montserrat_12, 0);
    lv_obj_set_style_text_font(pwd_label, &lv_font_siyuanheiti_normal_20, 0);
    lv_label_set_text(pwd_label, "点击连接后请等待约15秒...");                  // 为标签设置新文本

    // 选择域布局容器
    lv_obj_t * opt_panel = lv_obj_create(g_wifi_conn_info_panel);
    lv_obj_set_size(opt_panel, 220, 50);
    lv_obj_set_style_bg_opa(opt_panel, 0, 0);
    lv_obj_set_style_border_opa(opt_panel, 0, 0);
    lv_obj_set_layout(opt_panel, LV_LAYOUT_FLEX);
    lv_obj_set_flex_align(opt_panel, LV_FLEX_ALIGN_SPACE_EVENLY  , LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_flex_flow(opt_panel, LV_FLEX_FLOW_ROW);
    lv_obj_set_style_pad_all(opt_panel, 0, 0);

    // 退出按钮，也可以点击空白区域退出
    lv_obj_t * btn_cancel = lv_btn_create(opt_panel);
    lv_obj_set_size(btn_cancel, 100, 45);
    lv_obj_add_event_cb(btn_cancel, conn_btn_event_handler, LV_EVENT_CLICKED, "back");
    lv_obj_t * label_cancel = lv_label_create(btn_cancel);
    lv_obj_set_style_text_font(label_cancel, &lv_font_siyuanheiti_normal_20, 0);
    lv_label_set_text(label_cancel, "返回");
    lv_obj_center(label_cancel);

    // 连接wifi按钮
    lv_obj_t * btn_conn = lv_btn_create(opt_panel); 
    lv_obj_set_size(btn_conn, 100, 45);
    lv_obj_add_event_cb(btn_conn, conn_btn_event_handler, LV_EVENT_CLICKED, wifiName);
    lv_obj_t * label_conn = lv_label_create(btn_conn);
    lv_obj_set_style_text_font(label_conn, &lv_font_siyuanheiti_normal_20, 0);
    lv_label_set_text(label_conn, "连接");
    lv_obj_center(label_conn);



}


static void event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj));
    lv_obj_del(mbox1);
}



void msgbox_wlanInfo(void)
{
    char tempString[64] = {0};
    static const char * btns[] ={"确定", "取消", ""};

    char info[1024] = {0};

    //状态
    if(WLAN_info.cable == 0){
        strcat(info, "状态:未连接\n");
    }else{
        strcat(info, "状态:已连接\n");
    }
    
    //物理地址
    memset(tempString, 0, sizeof(tempString));
    lv_snprintf(tempString, sizeof(tempString), "物理地址MAC:%s", WLAN_info.mac);
    strcat(info, tempString);
    //IP
    memset(tempString, 0, sizeof(tempString));
    lv_snprintf(tempString, sizeof(tempString), "IP地址:%s", WLAN_info.ipaddr);
    strcat(info, tempString);
    //子网掩码
    memset(tempString, 0, sizeof(tempString));
    lv_snprintf(tempString, sizeof(tempString), "子网掩码:%s", WLAN_info.netmask);
    strcat(info, tempString);
    //网关
    memset(tempString, 0, sizeof(tempString));
    lv_snprintf(tempString, sizeof(tempString), "网关:%s", WLAN_info.gateway);
    strcat(info, tempString);

    mbox1 = lv_msgbox_create(NULL, "wifi信息", info, btns, false);
    lv_obj_set_size(mbox1, 500, 300);
    lv_obj_set_style_text_font(mbox1, &lv_font_siyuanheiti_normal_20, NULL);
    lv_obj_add_event_cb(mbox1, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_center(mbox1);
}




bool get_wlan0_info(void)
{
    char cmd_buff[128];
    char result[128];

    // 检查网线连接状态
    memset(cmd_buff, 0, sizeof(cmd_buff));
    memset(result, 0, sizeof(result));
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "cat /sys/class/net/wlan0/carrier");
    shell_opt_handle(cmd_buff, result, sizeof(result));
    
    WLAN_info.cable = atoi(result);
    LV_LOG_USER("WLAN_info.cable=%d", WLAN_info.cable);
    if (WLAN_info.cable == 1)
    {
        lv_snprintf(WLAN_info.device, sizeof(WLAN_info.device), "wlan0");
    }
    else
    {
        LV_LOG_USER("wlan0 are connected!\n");
        memset(WLAN_info.dhcp, 0, sizeof(WLAN_info.dhcp));
        memset(WLAN_info.mac, 0, sizeof(WLAN_info.mac));
        memset(WLAN_info.ipaddr, 0, sizeof(WLAN_info.ipaddr));
        memset(WLAN_info.gateway, 0, sizeof(WLAN_info.gateway));
        memset(WLAN_info.netmask, 0, sizeof(WLAN_info.netmask));
        return;
    }

    // 检查DHCP状态
    lv_snprintf(WLAN_info.dhcp, sizeof(WLAN_info.dhcp), "YES");

    // 获取MAC地址
    memset(cmd_buff, 0, sizeof(cmd_buff));
    memset(result, 0, sizeof(result));
    //awk默认分隔符为空白，$5输出分割后第五个字段
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "ifconfig %s | grep \"HWaddr \" | awk '{print $5}' | tr -d '\n'", WLAN_info.device); // ifconfig wlan0 | grep "HWaddr " | awk '{print $5}'
    shell_opt_handle(cmd_buff, result, sizeof(result));
    lv_snprintf(WLAN_info.mac, sizeof(WLAN_info.mac), result);

    // 获取IP地址
    memset(cmd_buff, 0, sizeof(cmd_buff));
    memset(result, 0, sizeof(result));
    //lv_snprintf(cmd_buff, sizeof(cmd_buff), "ifconfig %s | grep \"inet \" | awk '{print $2}'", WLAN_info.device);
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "ifconfig %s | grep \"inet \" | awk -F '[:]'  '{print $2}' | awk  '{print $1}'| tr -d '\n'", WLAN_info.device);  // ifconfig eth0 | grep "inet " | awk -F '[:]'  '{print $2}' | awk  '{print $1}'
    shell_opt_handle(cmd_buff, result, sizeof(result));
    lv_snprintf(WLAN_info.ipaddr, sizeof(WLAN_info.ipaddr), result);
    
    // 获取网关
    memset(cmd_buff, 0, sizeof(cmd_buff));
    memset(result, 0, sizeof(result));
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "ip route show  | grep \"default via \" | awk '{print $3}'| tr -d '\n'");  // ip route show  | grep "default via " | awk '{print $3}'
    shell_opt_handle(cmd_buff, result, sizeof(result));
    lv_snprintf(WLAN_info.gateway, sizeof(WLAN_info.gateway), result);

    // 获取子网掩码
    memset(cmd_buff, 0, sizeof(cmd_buff));
    memset(result, 0, sizeof(result));
    //lv_snprintf(cmd_buff, sizeof(cmd_buff), "ifconfig %s | grep \"netmask \" | awk '{print $4}'", WLAN_info.device);  
    lv_snprintf(cmd_buff, sizeof(cmd_buff), "ifconfig %s | grep \"Mask\" | awk -F '[:]'  '{print $4}'| tr -d '\n'", WLAN_info.device);// ifconfig eth0 | grep "Mask" | awk -F '[:]'  '{print $4}'
    shell_opt_handle(cmd_buff, result, sizeof(result));
    lv_snprintf(WLAN_info.netmask, sizeof(WLAN_info.netmask), result);

    memset(cmd_buff, 0, sizeof(cmd_buff));
    memset(result, 0, sizeof(result));
    
    return 1;
}

static void wifi_list_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);

    if(code == LV_EVENT_CLICKED) {
        
        lv_snprintf(wifiName, sizeof(wifiName), "%s", lv_list_get_btn_text(g_wifi_scan_list, obj));
        LV_LOG_USER("Clicked, wifiName=%s\n", wifiName);

        wifi_conn_dialog();
    }
}

void getSaveWifiName(char *saveWifiName){
    FILE *fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read = 0;

    fp = fopen("/appconfigs/wpa_supplicant.conf", "r");//只读打开
    if (fp == NULL) {
        perror("fopen");
        return;
    }
    line = NULL;
    while ((read = getline(&line, &len, fp)) != -1) {
        //读一行文本，直到 fp 的结束位置
        
        char* result = strstr(line, "ssid=\"");
        if(result == NULL){
            if (line) {
                free(line);
            }
            line = NULL;
            continue;
        }
        
        char *p = result + strlen("ssid=\"");
        int i = 0;
        while (*p != '\"'){
            //LV_LOG_USER("*p=%c", *p);
            saveWifiName[i] = *p;
            i++;
            p++;
        }
        LV_LOG_USER("saveWifiName=%s", saveWifiName);//
        if (line) {
            free(line);
        }
        line = NULL;
        break;
    }

    if (fclose(fp) == EOF) {
        perror("fclose");
        return;
    }

}

void getSaveWifiPassword(char *password){
    FILE *fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read = 0;

    fp = fopen("/appconfigs/wpa_supplicant.conf", "r");//只读打开
    if (fp == NULL) {
        perror("fopen");
        return;
    }
    line = NULL;
    while ((read = getline(&line, &len, fp)) != -1) {
        //读一行文本，直到 fp 的结束位置
        
        char* result = strstr(line, "psk=\"");
        if(result == NULL){
            if (line) {
                free(line);
            }
            line = NULL;
            continue;
        }
        
        char *p = result + strlen("psk=\"");
        int i = 0;
        while (*p != '\"'){
            //LV_LOG_USER("*p=%c", *p);
            password[i] = *p;
            i++;
            p++;
        }
        LV_LOG_USER("password=%s", password);//
        if (line) {
            free(line);
        }
        line = NULL;
        break;
    }

    if (fclose(fp) == EOF) {
        perror("fclose");
        return;
    }

}

static void set_cell_value(lv_obj_t * table)
{
    get_wlan0_info();
    // Fill the first column
    lv_table_set_cell_value(table, 0, 0, "状态");
    lv_table_set_cell_value(table, 1, 0, "DHCP");
    lv_table_set_cell_value(table, 2, 0, "物理地址MAC");
    lv_table_set_cell_value(table, 3, 0, "IP地址");
    lv_table_set_cell_value(table, 4, 0, "网关");
    lv_table_set_cell_value(table, 5, 0, "子网掩码");

    // Fill the second column
    lv_table_set_cell_value(table, 0, 1, "已连接"); // LV_SYMBOL_OK LV_SYMBOL_WARNING
    lv_table_set_cell_value(table, 1, 1, WLAN_info.dhcp);
    lv_table_set_cell_value(table, 2, 1, WLAN_info.mac);
    lv_table_set_cell_value(table, 3, 1, WLAN_info.ipaddr);
    lv_table_set_cell_value(table, 4, 1, WLAN_info.gateway);
    lv_table_set_cell_value(table, 5, 1, WLAN_info.netmask);

    
    if (WLAN_info.cable == 1){
        lv_table_set_cell_value(table, 0, 1, "已连接"); // LV_SYMBOL_OK LV_SYMBOL_WARNING
    }else
    {
        lv_table_set_cell_value(table, 0, 1, "未连接");    // LV_SYMBOL_OK LV_SYMBOL_WARNING
    }
}

void imx6ull_set_wlan_init(void)
{
    lv_obj_t * obj_layout;
    lv_obj_t * ctr_btn_layout;
    static lv_style_t obj_layout_style;   // 容器的样式
    static lv_style_t ctr_btn_layout_style;   // 容器的样式
    lv_obj_t * label;
    LV_LOG_USER("init enter");

    lv_style_init(&obj_layout_style);
    //lv_style_set_pad_all(&obj_layout_style, 0);
    //lv_style_set_bg_opa(&obj_layout_style, 0);
    //lv_style_set_shadow_opa(&obj_layout_style, 0);
    lv_style_set_border_opa(&obj_layout_style, 0);

    lv_style_init(&ctr_btn_layout_style);
    lv_style_set_pad_all(&ctr_btn_layout_style, 0);
    //lv_style_set_bg_opa(&ctr_btn_layout_style, 0);
    //lv_style_set_shadow_opa(&ctr_btn_layout_style, 0);
    lv_style_set_border_opa(&ctr_btn_layout_style, 0);


    /* Layout Init */
    obj_layout = lv_obj_create(lv_scr_act());
    lv_obj_set_size(obj_layout,  LV_PCT(100), LV_PCT(100));
    lv_obj_set_layout(obj_layout, LV_LAYOUT_FLEX);
    lv_obj_set_style_base_dir(obj_layout, LV_BASE_DIR_RTL, 0);//函数设置从右向左的书写方式
    lv_obj_set_flex_flow(obj_layout, LV_FLEX_FLOW_COLUMN);
    lv_obj_center(obj_layout);
    lv_obj_add_style(obj_layout, &obj_layout_style, 0);

    /* Create title */
    lv_obj_t * label_title = lv_label_create(obj_layout);
    lv_obj_set_width(label_title, LV_PCT(100));
    lv_obj_set_style_text_font(label_title, &lv_font_montserrat_32, 0);
    lv_obj_set_style_text_align(label_title, LV_TEXT_ALIGN_CENTER, 0);
    //lv_label_set_recolor(label_title, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_text(label_title, "Wi-Fi");
    lv_obj_align(label_title, LV_ALIGN_CENTER, 0, 0); //lv_obj_center(label_title);

    ctr_btn_layout = lv_obj_create(obj_layout);
    lv_obj_set_size(ctr_btn_layout,  LV_PCT(100), 40);
    lv_obj_set_layout(ctr_btn_layout, LV_LAYOUT_FLEX);
    lv_obj_set_style_base_dir(ctr_btn_layout, LV_BASE_DIR_LTR, 0);
    lv_obj_set_flex_flow(ctr_btn_layout, LV_FLEX_FLOW_ROW);
    lv_obj_center(ctr_btn_layout);
    lv_obj_add_style(ctr_btn_layout, &ctr_btn_layout_style, 0);

    lv_obj_t * btn1 = lv_btn_create(ctr_btn_layout);
    lv_obj_set_size(btn1, 100, 40);
    lv_obj_add_event_cb(btn1, refresh_wifi_list_event_handler, LV_EVENT_CLICKED, NULL);
    label = lv_label_create(btn1);
    lv_label_set_text(label, LV_SYMBOL_REFRESH);
    lv_obj_align(label, LV_ALIGN_CENTER, -20, 0);//对齐到其父组件的中心
    lv_obj_t * label_1 = lv_label_create(btn1);
    lv_obj_set_style_text_font(label_1, &lv_font_siyuanheiti_normal_20, 0);
    lv_label_set_text(label_1, "刷新");
    lv_obj_align(label_1, LV_ALIGN_CENTER, 10, 0);



    label = lv_label_create(obj_layout);
    lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP);
    lv_obj_set_width(label, 150);
    lv_obj_set_style_text_font(label, &lv_font_siyuanheiti_normal_20, LV_PART_MAIN);
    lv_label_set_text(label, "已保存");
    /*Create wifi connected list*/
    g_wifi_conn_list = lv_list_create(obj_layout);
    lv_obj_set_size(g_wifi_conn_list, LV_PCT(100), LV_PCT(10));
    lv_obj_center(g_wifi_conn_list);
    /*Add buttons to the list*/
    char saveWifiName[32] = {0};
    getSaveWifiName(saveWifiName);
    lv_obj_t * btn = lv_list_add_btn(g_wifi_conn_list, LV_SYMBOL_WIFI, saveWifiName);
    /* NET info */
    lv_obj_t * g_table_lan_info = lv_table_create(obj_layout);
    lv_obj_set_width(g_table_lan_info, LV_PCT(100));
    lv_obj_set_style_pad_all(g_table_lan_info, 0, 0);
    lv_obj_set_style_border_opa(g_table_lan_info, 0, 0);
    lv_obj_set_style_text_font(g_table_lan_info, &lv_font_siyuanheiti_normal_20, 0);  // 中文
    lv_table_set_col_width(g_table_lan_info, 0, 710);  // 150
    lv_table_set_col_width(g_table_lan_info, 1, 220);  // 800
    lv_obj_set_style_base_dir(g_table_lan_info, LV_BASE_DIR_LTR, 0);//函数设置从左向右的书写方式
    // Fill the table
    set_cell_value(g_table_lan_info);




    label = lv_label_create(obj_layout);
    lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP);
    lv_obj_set_width(label, 150);
    lv_obj_set_style_text_font(label, &lv_font_siyuanheiti_normal_20, LV_PART_MAIN);
    lv_label_set_text(label, "可用wifi");
    /*Create wifi scan list*/
    g_wifi_scan_list = lv_list_create(obj_layout);
    lv_obj_set_size(g_wifi_scan_list, LV_PCT(100), LV_PCT(60));
    lv_obj_center(g_wifi_scan_list);
    wlan_scan();
}

static void eventHandlerReturn(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");

        /*注意 不要删除当前活动的屏幕。在删除屏幕之前，确保另一个屏幕已经切换到活动状态。
        如果你确实需要删除当前屏幕，
        第一步，在删除之前切换到另一个屏幕，
        第二步，调用 lv_timer_del()，删除之前屏幕的 Timers（定时器）
        第三步，调用 lv_obj_del()，删除之前的屏幕
        */
        //返回主界面
        lv_scr_load(screenMain);

        //lv_timer_pause(timerClock);//暂停定时器
        //lv_timer_del(timerClock);//删除定时器
        //销毁 set WLAN 界面
        lv_obj_del(screenWLAN);
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

void lv_setWLANReturn(void)
{
    lv_obj_t * label;

    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn1, eventHandlerReturn, LV_EVENT_ALL, NULL);
    lv_obj_set_size(btn1, 120, 50);
    lv_obj_align(btn1, LV_ALIGN_TOP_RIGHT, -20, 20);

    label = lv_label_create(btn1);
    lv_obj_set_style_text_font(label, &lv_font_siyuanheiti_normal_20, 0);
    lv_label_set_text(label, "返回");
    lv_obj_center(label);
}

void set_wlan(lv_obj_t * screen){
    LV_LOG_USER("enter");
    screenMain = screen;

    //创建设置时间屏幕
    screenWLAN = lv_obj_create(NULL);
    lv_scr_load(screenWLAN);

    imx6ull_set_wlan_init();

    //返回按钮
    lv_setWLANReturn();    
}

#endif /* LV_USE_100ASK_DEMO_GAME_2048 */
